package com.baobao.dao;

import com.baobao.domain.Student;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage Student entities.
 * 
 */
@Repository("StudentDAO")
@Transactional
public class StudentDAOImpl extends AbstractJpaDao<Student> implements
		StudentDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Student.class }));

	/**
	 * EntityManager injected by Spring for persistence unit BaoBao
	 *
	 */
	@PersistenceContext(unitName = "BaoBao")
	private EntityManager entityManager;

	/**
	 * Instantiates a new StudentDAOImpl
	 *
	 */
	public StudentDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findStudentByClassId
	 *
	 */
	@Transactional
	public Set<Student> findStudentByClassId(String classId) throws DataAccessException {

		return findStudentByClassId(classId, -1, -1);
	}

	/**
	 * JPQL Query - findStudentByClassId
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Student> findStudentByClassId(String classId, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findStudentByClassId", startResult, maxRows, classId);
		return new LinkedHashSet<Student>(query.getResultList());
	}

	/**
	 * JPQL Query - findStudentByBbIdContaining
	 *
	 */
	@Transactional
	public Set<Student> findStudentByBbIdContaining(String bbId) throws DataAccessException {

		return findStudentByBbIdContaining(bbId, -1, -1);
	}

	/**
	 * JPQL Query - findStudentByBbIdContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Student> findStudentByBbIdContaining(String bbId, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findStudentByBbIdContaining", startResult, maxRows, bbId);
		return new LinkedHashSet<Student>(query.getResultList());
	}

	/**
	 * JPQL Query - findStudentByClassIdContaining
	 *
	 */
	@Transactional
	public Set<Student> findStudentByClassIdContaining(String classId) throws DataAccessException {

		return findStudentByClassIdContaining(classId, -1, -1);
	}

	/**
	 * JPQL Query - findStudentByClassIdContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Student> findStudentByClassIdContaining(String classId, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findStudentByClassIdContaining", startResult, maxRows, classId);
		return new LinkedHashSet<Student>(query.getResultList());
	}

	/**
	 * JPQL Query - findStudentByName
	 *
	 */
	@Transactional
	public Set<Student> findStudentByName(String name) throws DataAccessException {

		return findStudentByName(name, -1, -1);
	}

	/**
	 * JPQL Query - findStudentByName
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Student> findStudentByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findStudentByName", startResult, maxRows, name);
		return new LinkedHashSet<Student>(query.getResultList());
	}

	/**
	 * JPQL Query - findStudentBySex
	 *
	 */
	@Transactional
	public Set<Student> findStudentBySex(Integer sex) throws DataAccessException {

		return findStudentBySex(sex, -1, -1);
	}

	/**
	 * JPQL Query - findStudentBySex
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Student> findStudentBySex(Integer sex, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findStudentBySex", startResult, maxRows, sex);
		return new LinkedHashSet<Student>(query.getResultList());
	}

	/**
	 * JPQL Query - findStudentByPrimaryKey
	 *
	 */
	@Transactional
	public Student findStudentByPrimaryKey(String id) throws DataAccessException {

		return findStudentByPrimaryKey(id, -1, -1);
	}

	/**
	 * JPQL Query - findStudentByPrimaryKey
	 *
	 */

	@Transactional
	public Student findStudentByPrimaryKey(String id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findStudentByPrimaryKey", startResult, maxRows, id);
			return (com.baobao.domain.Student) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findAllStudents
	 *
	 */
	@Transactional
	public Set<Student> findAllStudents() throws DataAccessException {

		return findAllStudents(-1, -1);
	}

	/**
	 * JPQL Query - findAllStudents
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Student> findAllStudents(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllStudents", startResult, maxRows);
		return new LinkedHashSet<Student>(query.getResultList());
	}

	/**
	 * JPQL Query - findStudentById
	 *
	 */
	@Transactional
	public Student findStudentById(String id) throws DataAccessException {

		return findStudentById(id, -1, -1);
	}

	/**
	 * JPQL Query - findStudentById
	 *
	 */

	@Transactional
	public Student findStudentById(String id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findStudentById", startResult, maxRows, id);
			return (com.baobao.domain.Student) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findStudentByIdContaining
	 *
	 */
	@Transactional
	public Set<Student> findStudentByIdContaining(String id) throws DataAccessException {

		return findStudentByIdContaining(id, -1, -1);
	}

	/**
	 * JPQL Query - findStudentByIdContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Student> findStudentByIdContaining(String id, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findStudentByIdContaining", startResult, maxRows, id);
		return new LinkedHashSet<Student>(query.getResultList());
	}

	/**
	 * JPQL Query - findStudentByNameContaining
	 *
	 */
	@Transactional
	public Set<Student> findStudentByNameContaining(String name) throws DataAccessException {

		return findStudentByNameContaining(name, -1, -1);
	}

	/**
	 * JPQL Query - findStudentByNameContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Student> findStudentByNameContaining(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findStudentByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Student>(query.getResultList());
	}

	/**
	 * JPQL Query - findStudentByBbId
	 *
	 */
	@Transactional
	public Set<Student> findStudentByBbId(String bbId) throws DataAccessException {

		return findStudentByBbId(bbId, -1, -1);
	}

	/**
	 * JPQL Query - findStudentByBbId
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Student> findStudentByBbId(String bbId, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findStudentByBbId", startResult, maxRows, bbId);
		return new LinkedHashSet<Student>(query.getResultList());
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(Student entity) {
		return true;
	}
}
